// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fowo.api.entity.BundledProduct;
import com.fowo.api.entity.ProductSub;
import com.fowo.api.model.bundled.product.BundledProductVo;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * 捆绑产品 服务实现类
 */
@Service
public class BundledProductServiceImpl extends BundledProductBaseServiceImpl {

    /**
     * 创建捆绑产品
     * @param model 捆绑产品
     * @return 新数据的主键
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(BundledProductVo model) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        BundledProduct entity = new BundledProduct();
        BeanUtils.copyProperties(model, entity);
        // 设置常量字段
        entity.setType("CP05");
        entity.setCreateTime(new Date());
        entity.setLastUpdateTime(new Date());
        BundledProduct one = this.getOne(Wrappers.<BundledProduct>query().lambda().eq(BundledProduct::getSku, entity.getSku()).eq(BundledProduct::getType,entity.getType()),false);
        Assert.isNull(one,"sku已存在！");

        if(!CollectionUtils.isEmpty(model.getSubProducts())){
            BigDecimal procurementCost = model.getSubProducts().stream()
                    .map(s -> s.getProcurementCost())
                    .filter(b -> null != b)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            entity.setProcurementCost(procurementCost);
        }
        if (currentUser != null) {
            entity.setCreateUser(currentUser.getUserId());
            entity.setLastUpdateUser(currentUser.getUserId());
        }
        if (this.save(entity)) {
            model.setId(entity.getId());
            updateAllChilds(model);
            return entity.getId();
        }
        throw new Exception("捆绑产品保存失败");
    }

    /**
     * 更新捆绑产品
     * @param model 捆绑产品
     * @return 更新是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(BundledProductVo model) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        BundledProduct entity = this.getById(model.getId());
        if (entity == null || !Objects.equals(entity.getType(), "CP05")) {
            throw new Exception("捆绑产品不存在");
        }
        BeanUtils.copyProperties(
                model,
                entity,
                "lastUpdateUser",
                "lastUpdateTime",
                "createUser",
                "createTime",
                "id"
        );
        entity.setLastUpdateTime(new Date());
        if(!CollectionUtils.isEmpty(model.getSubProducts())){
            BigDecimal procurementCost = model.getSubProducts().stream()
                    .map(s -> s.getProcurementCost())
                    .filter(b -> null != b)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            entity.setProcurementCost(procurementCost);
        }
        if (currentUser != null) {
            entity.setLastUpdateUser(currentUser.getUserId());
        }
        if (this.updateById(entity)) {
            updateAllChilds(model);
            return true;
        }
        return false;
    }

}
